/* pages/customer/customer.ts */
interface CustomerInfo {
    name: string;
    phone: string;
    email?: string;
    address?: string;
  }
  
  Page({
    data: {
      users: [] as CustomerInfo[],
    },
  
    onLoad() {
      this.loadUsers();
    },
  
    onShow() {
      this.loadUsers(); // 每次页面显示时重新加载用户列表
    },
  
    loadUsers() {
      const users = wx.getStorageSync('users') || [];
      this.setData({
        users
      });
    },
  
    goToCustomerInfo(event: WechatMiniprogram.TouchEvent) {
      const { index } = event.currentTarget.dataset;
      const user = this.data.users[index];
      wx.navigateTo({
        url: '/pages/customerInfo/customerInfo',
        success: (res) => {
          res.eventChannel.emit('editCustomer', user);
        }
      });
    },
  
    deleteCustomer(event: WechatMiniprogram.TouchEvent) {
      const { index } = event.currentTarget.dataset;
      const users = this.data.users;
      wx.showModal({
        title: '提示',
        content: '确定要删除这个用户吗？',
        success: (res) => {
          if (res.confirm) {
            users.splice(index, 1);
            this.setData({
              users
            });
            wx.setStorageSync('users', users);
            wx.showToast({
              title: '用户删除成功',
              icon: 'success',
              duration: 2000
            });
          }
        }
      });
    }
  });
  